//
//  RollSelect.m
//  dyseller
//
//  Created by 红喇叭 on 2017/11/7.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "RollSelect.h"

@implementation RollSelect{
    
}

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)data{
    self = [super initWithFrame:frame];
    if(self){
        _dataArr = data;
        [self addtopView];
        [self addSubview:self.pivkerView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)addtopView{
    UIButton *leftbtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 60, 30)];
    [leftbtn setTitleColor:[UIColor blackColor] forState:0];
    [leftbtn setTitle:@"取消" forState:0];
    [leftbtn addTarget:self action:@selector(cancelView) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftbtn];
    UIButton *rightbtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-70, 5, 60, 30)];
    [rightbtn setTitleColor:[UIColor blackColor] forState:0];
    [rightbtn addTarget:self action:@selector(selectValue) forControlEvents:UIControlEventTouchUpInside];
    [rightbtn setTitle:@"确定" forState:0];
    [self addSubview:rightbtn];
    
}

- (void)cancelView{
    [self removeFromSuperview];
}

- (void)selectValue{
    NSInteger row=[self.pivkerView selectedRowInComponent:0];
    NSString *dateStr=[self.dataArr objectAtIndex:row];
    _blcok(dateStr);
}

- (UIPickerView *)pivkerView{
    if(_pivkerView == nil){
        _pivkerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.width, self.height-40)];
        _pivkerView.delegate = self;
        _pivkerView.dataSource = self;
        _pivkerView.backgroundColor = UIColorFromRGB(0xf8f8f8);
    }
    return _pivkerView;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return  _dataArr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return (NSString *)(_dataArr[row]);
}

@end
